/*
 * FileName: KafkaUtil.java
 * Author:   zzw
 * Date:     2018年08月16日
 * Description:
 */
package com.zzw.utils;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.List;
import java.util.Properties;

/**
 * 〈kafka 工具类〉<br>
 * 〈〉
 *
 * @author zzw
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 */
public class KafkaUtil {

    private static Properties properties;

    static {
        properties = new Properties();
        properties.put("bootstrap.servers", "escnode0:9092");
        properties.put("acks", "all");
        properties.put("retries", 0);
        properties.put("batch.size", 16384);
        properties.put("linger.ms", 1);
        properties.put("buffer.memory", 33554432);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    }

    private static Producer producer = new KafkaProducer<String, String>(KafkaUtil.properties);

    public static void send(String topic,String msg){
        try {
            producer.send(new ProducerRecord<String, String>(topic, msg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send(String topic, List<String> msgs){
        try {
            for (String msg:msgs){
                producer.send(new ProducerRecord<String, String>(topic, msg));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 1000;i < 2000;i++){
            send("test6","hello" + i);
        }
      producer.close();
    }
}
